<template>
	<view class="">
		<view class="hint">
			<image src="https://statics.xmcsrv.net/weixin/setting/warn.png" mode="widthFix" />
			<view class="text1">{{title1}}</view>
			<view class="text2">{{title2}}</view>
		</view>
		<view class="box" v-if="functions == 'UnlockDirection' || functions == 'UnlockMode'">
			<radio-group>
				<view @tap="jftapUnlock(item,index)" class="item" :class="list.length-1 == index"
					v-for="(item,index) in list" :key="index">
					<view class="item-top">
						<view class="title">
							{{item.title}}
							<view v-if="item.subtitle" class="subtitle">
								{{item.subtitle}}
							</view>
						</view>
						<view class="">
							<radio :value="item.value" color="#FF0025"
								:checked="item.value === (functions == 'UnlockDirection'?DoorConfig.UnlockDirection.Direction:DoorConfig.UnlockMode.mode)" />
						</view>
					</view>
					<view class="item-img">
						<image v-for="(izem,indez) in item.imgUrl" :style="{width:item.imgWidth+'%',height:'auto'}"
							:src="izem" :key="indez" mode="widthFix" />
					</view>
				</view>
			</radio-group>
		</view>
		<view class="box box2" v-if="functions == 'MotorTorque'">
			<view class="emImg">
				<image src="https://statics.xmcsrv.net/weixin/setting/motorAdjustment.png" mode="widthFix"></image>
			</view>
			<radio-group>
				<view @tap="jftapMotorTorque(item,index)" class="item" :class="list.length-1 == index"
					v-for="(item,index) in motortorqueRegulationList" :key="index">
					<view class="item-top">
						<view class="title">
							{{item.title}}
						</view>
						<view class="">
							<radio :value="item.value" color="#FF0025"
								:checked="item.value === DoorConfig.MotorTorque.Torque" />
						</view>
					</view>
				</view>
			</radio-group>
		</view>
		<view class="box box2 box3" v-if="functions == 'NormalOpenMode'">
			<view class="item" :class="list.length-1 == index">
				<view class="item-top">
					<view class="title">
						常开模式
					</view>
					<view class="">
						<switch :checked="normallyOpenMode" color="#FF0025" style="transform:scale(0.9)"
							@change="NormalOpenModeChange" />
					</view>
				</view>
			</view>
			<view class="emImg">
				<image src="https://statics.xmcsrv.net/weixin/setting/openMode.png" mode="widthFix"></image>
			</view>
		</view>
	</view>
</template>

<script>
	const {
		turnTenToSixteen
	} = require('@/utils/util.js');
	const {
		handledoorLockTransparent
	} = require('@/jvss/pagesDevice/utils/index.js');
	export default {
		data() {
			return {
				title1: '',
				title2: '',
				list: [],
				motortorqueRegulationList: [{
					title: '小',
					value: 0,
				}, {
					title: '中',
					value: 1,
				}, {
					title: '大',
					value: 2,
				}, ],
				// 常开模式
				normallyOpenMode: false,
				DoorConfig: null,
				device: null,
				settingType: null,
				functions: null
			}
		},
		onLoad(opt) {
			console.log(opt, 'opt')
			this.device = opt
			this.settingType = opt.type;
			this.functions = opt.function
			this.hlandOpt(opt.function)
			this.doorLockTransparent();
			uni.setNavigationBarTitle({
				title: opt.title,
			});
		},
		methods: {
			hlandOpt(type) {
				const titleMap = {
					'UnlockDirection': {
						title1: '慎重修改项',
						title2: '根据门的开门方向选择，确认后请勿再次修改'
					},
					'MotorTorque': {
						title1: '慎重修改项',
						title2: '开门时电机转动力度(如出现锁舌开关不到位，可增大扭力)'
					},
					'UnlockMode': {
						title1: '注意',
						title2: '如需要使用两个验证方式开锁，请选择组合模式'
					},
					'NormalOpenMode': {
						title1: '注意',
						title2: '开启该功能后，任何人使用门锁上的任意验证方式都将直接打开门锁，请慎重使用'
					}
				};
				const titles = titleMap[type] || {};
				this.title1 = titles.title1;
				this.title2 = titles.title2;
				const listMap = {
					'UnlockDirection': [{
							title: '向左开',
							imgUrl: ['https://statics.xmcsrv.net/weixin/setting/leftOpen1.png',
								'https://statics.xmcsrv.net/weixin/setting/leftOpen2.png'
							],
							imgWidth: 40,
							value: 0
						},
						{
							title: '向右开',
							imgUrl: ['https://statics.xmcsrv.net/weixin/setting/rightOpen1.png',
								'https://statics.xmcsrv.net/weixin/setting/rightOpen2.png'
							],
							imgWidth: 40,
							value: 1
						}
					],
					'UnlockMode': [{
							title: '单开模式',
							imgUrl: ['https://statics.xmcsrv.net/weixin/setting/singleOpenMode.png'],
							subtitle: "单个用户验证后开锁",
							imgWidth: 90,
							value: 0
						},
						{
							title: '组合模式',
							imgUrl: ['https://statics.xmcsrv.net/weixin/setting/combination.png'],
							subtitle: "单个管理员验证或两种普通方式验证后开锁",
							imgWidth: 90,
							value: 1
						}
					]
				};
				this.list = listMap[type] || [];
			},
			doorLockTransparent() {
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				let data = {
					Name: 'OPDoorLockProCmd',
					OPDoorLockProCmd: {
						Cmd: "GetDoorConfig"
					}
				};
				let token = this.device.token;
				console.log(this.DoorConfig)
				getApp().globalData.api.doorLockTransparent(data, token, (result) => {
					uni.hideLoading()
					console.log('门锁获取设置：', result);
					handledoorLockTransparent(result, (res, Err) => {
						let DoorConfig = res
						this.DoorConfig = DoorConfig ? DoorConfig : null
						this.normallyOpenMode = this.settingType == 8 ? DoorConfig.NormalOpenMode.Mode ==
							0 ? false :
							true : false
					}, false, () => {}, 'DoorConfig')
					console.log(this.DoorConfig)
				}, false);
			},
			// 开锁方向 开锁模式
			jftapUnlock(e, index) {
				if (this.functions == 'UnlockDirection') {
					this.DoorConfig.UnlockDirection.Direction = e.value;
					this.save('UnlockDirection', this.DoorConfig.UnlockDirection)
				} else {
					this.DoorConfig.UnlockMode.mode = e.value;
					this.save('UnlockMode', this.DoorConfig.UnlockMode);
				}
			},
			// 电机扭矩调节
			jftapMotorTorque(e, index) {
				this.DoorConfig.MotorTorque.Torque = e.value
				this.save('MotorTorque', this.DoorConfig.MotorTorque)
			},
			// 常开模式
			NormalOpenModeChange(e) {
				if (e.detail.value == false) {
					this.DoorConfig.NormalOpenMode.mode = 0
				} else {
					this.DoorConfig.NormalOpenMode.mode = 1
				}
				this.save('NormalOpenMode', this.DoorConfig.NormalOpenMode)
			},
			// 设置
			save(type, value) {
				console.log(type, value)
				let token = this.device.token;
				let data = {
					Name: "OPDoorLockProCmd",
					OPDoorLockProCmd: {
						Cmd: "SetDoorConfig",
						DoorConfig: {}
					}
				};
				data.OPDoorLockProCmd.DoorConfig[type] = value
				uni.showLoading({
					title: '设置中...'
				});
				getApp().globalData.api.doorLockTransparent(data, token, (result) => {
					uni.hideLoading()
					handledoorLockTransparent(result, (res, Err) => {
						if (Err === '0x00') {
							uni.showToast({
								title: '保存成功'
							})
						} else {
							uni.showToast({
								icon: 'error',
								title: '保存失败'
							})
						}
					}, true, () => {}, 'SetDoorConfig')
					console.log('门锁设置：', result);
				}, false);
			}
		}
	}
</script>

<style lang="scss">
	.hint {
		text-align: center;

		image {
			width: 120rpx;
			margin-top: 60rpx;
			height: auto;
		}

		view {
			color: #FF0000;
			margin: 10rpx 0;
		}

		.text1 {
			font-size: 32rpx;
			font-weight: 500;
			color: #FF0000;
		}

		.text2 {
			font-size: 26rpx;
		}
	}

	.box {
		margin-top: 30rpx;

		.item {
			background-color: #fff;
			width: 95%;
			border-radius: 20rpx;
			margin: 20rpx auto;
			padding: 20rpx;
			box-sizing: border-box;
		}

		.item-top {
			display: flex;
			justify-content: space-between;
			align-items: center;

			.title {
				font-size: 30rpx;
				color: #000000;
			}

			.subtitle {
				font-size: 24rpx;
				color: #898989;
			}
		}

		.item-img {
			padding: 20rpx 0;
			display: flex;
			justify-content: space-around;
		}

	}

	.box2 {
		background-color: #fff;
		width: 95%;
		border-radius: 20rpx;
		margin: 20rpx auto;

		.emImg {
			width: 80%;
			margin: auto;
			padding: 10rpx 0;
		}

		.item {
			margin: 0 auto;
		}
	}
</style>